package control;
import java.lang.String;


/**
 * XmlParser
 * @author Adriano de Albuquerque Batista
 * @author Alfeu Buriti
 * @author Pablo Diego Alencar
 * @author Yuri Cariry Cabral de Melo
 *
 */
public class XmlParser {


	/**
	 * getElementByName
	 *  le conteudo xml da etiqueta tag
	 * @param xml
	 * @param tag
	 * @return resultado conteudo do tag do xml
	 */
	public String getElementByName(String xml, String tag) {
		System.out.println("Inicio do XmlParser -> getElementByName ");

		int beginIndex;
		int endIndex;
		String resultado;

		// acessa String xml
		xml = xml.replaceAll("<" + tag + ".*?>", "<" + tag + ">");
		xml = xml.replaceAll("</" + tag + ".*?>", "</" + tag + ">");
		String tagOpen = "<" + tag + ">";
		String tagEnd = "</" + tag + ">";

		// checa se as tags existem
		if (xml.contains(tagOpen) && xml.contains(tagEnd)) {
			beginIndex = xml.indexOf(tagOpen);
			endIndex = xml.indexOf(tagEnd);
			resultado = xml.substring(beginIndex + tagOpen.length(),
					endIndex);
			System.out.println("Fim do XmlParser -> getElementByName ");	

			return resultado;
		} else {
			System.err.println(xml);
			System.err.println(tagOpen + " nao existe");
			System.err.println(tagEnd + " nao existe");
			System.exit(1);
			System.out.println("Fim do XmlParser -> getElementByName ");	
			return null;
		} 
	}


	/**
	 * cleanSoapMessage
	 * @param str String str
	 * @return str str
	 */
	public String cleanSoapMessage(String str) {
		System.out.println("Inicio do XmlParser -> cleanSoapMessage ");	

		str = str.replaceAll("&amp;", "&");
		str = str.replaceAll("&gt;", ">");
		str = str.replaceAll("&lt;", "<");

		System.out.println("Inicio do XmlParser -> cleanSoapMessage ");			
		return str;
	}

}